home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-09-16 | 6.0 KB | 146 lines | [TEXT/CCL2] |
- ;;-*- Mode: Lisp; Package: CCL -*-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;
- ;; mac-file-io.lisp
- ;;
- ;;Copyright © 1990, Apple Computer, Inc
- ;;
-
- ;; This file implements something similar to the high-level file I/O
- ;; primitives in Inside Macintosh.
- ;; It does NOT support asynchronous I/O (and neither does the Macintosh, really).
-
- ;; Routines that take an errorp parameter will signal an error if
- ;; the parameter is unspecified or true, otherwise, if there is an
- ;; error they return two values: NIL & the error number.
- ;; If there is no error, routines return one or more values the
- ;; first of which is non-NIL.
-
- ;;;;;;;;;;;;;
- ;;
- ;; Modification History
- ;;
- ;;
- ;; 04/28/93 mwp Release
- ;; 03/24/92 wkf Added GetVInfo lisp version based on Inside Mac Volume IV p107
- ;; ------------- 2.0
- ;; 02/27/92 bill fsopen returns a useful value again.
- ;; 02/23/92 gb Use newer traps, records, constants.
- ;; 02/12/92 bill fsopen gets a resolve-aliases-p parameter
- ;; ------------- 2.0f2
- ;; 12/31/91 bill use ccl::%err-disp in maybe-file-error
- ;; ------------- 2.0b4
- ;; 10/08/91 bill Move to CCL package
- ;; 09/05/91 bill no longer (require :records)
- ;; 08/24/91 gb Use new trap syntax so no more 1.3.2.
- ;; 08/19/91 bill in FSOpen: (%put-word paramBlock $fsAtMark $ioPosOffset) ->
- ;; (%put-word paramBlock $fsAtMark $ioPosMode)
- ;; (thanx to Dale J. Skrien)
- ;;
-
- (in-package :ccl)
-
- (eval-when (:compile-toplevel :load-toplevel :execute)
- (export '(with-FSOpen-file FSOpen FSClose FSRead FSWrite setFPos getFPos getEOF)))
-
- (defmacro with-FSOpen-file ((pb filename &optional read-write-p (vrefnum 0))
- &body body)
- `(let ((,pb (FSOpen ,filename ,read-write-p ,vrefnum)))
- (unwind-protect
- (progn ,@body)
- (FSClose ,pb))))
-
- ; Returns a paramBlock for doing furthur I/O with the file
- (defun FSOpen (filename &optional read-write-p (vrefnum 0) (errorp t)
- (resolve-aliases-p t))
- (when resolve-aliases-p (setq filename (truename filename)))
- (let ((paramBlock (make-record :hparamblockrec))
- ok)
- (unwind-protect
- (with-pstrs ((pname (mac-namestring filename)))
- (setf (pref paramblock :hparamblockrec.ioNameptr) pname
- (pref paramblock :hparamblockrec.ioVrefnum) vrefnum
- (pref paramblock :hparamblockrec.ioVersNum) 0
- (pref paramblock :hparamblockrec.ioPermssn) (if read-write-p #$fsRdWrPerm #$fsRdPerm)
- (pref paramblock :hparamblockrec.ioMisc) (%null-ptr))
- (#_Open paramBlock)
- (let ((res (pref paramBlock :hparamblockrec.ioResult)))
- (if (eql #$NoErr res)
- (progn
- (setf (pref paramblock :hparamblockrec.ioPosOffSet) 0
- (pref paramblock :hparamblockrec.ioPosMode) #$fsAtMark)
- (setq ok t)
- paramBlock)
- (maybe-file-error errorp res filename))))
- (unless ok
- (#_DisposePtr paramBlock)))))
-
- (defun FSClose (paramBlock &optional (errorp t))
- (#_Close paramBlock)
- (let ((errnum (pref paramBlock :hparamblockrec.ioResult)))
- (#_DisposePtr paramBlock)
- (or (eql errnum #$noErr)
- (maybe-file-error errorp errnum))))
-
- ; Returns two values: the number of bytes actually read, and the
- ; location of the file mark.
- (defun fsRead (paramBlock count buffer &optional (offset 0) (errorp t))
- (setf (pref paramBlock :hparamblockrec.ioBuffer) (%inc-ptr buffer offset)
- (pref paramBlock :hparamblockrec.ioReqCount) count)
- (#_Read paramBlock)
- (setf (pref paramBlock :hparamblockrec.ioPosMode) #$fsAtMark)
- (let ((errnum (pref paramBlock :hparamblockrec.ioResult)))
- (if (or (eql #$noErr errnum) (eql #$eofErr errnum))
- (values (pref paramBlock :hparamblockrec.ioActCount)
- (pref paramBlock :hparamblockrec.ioPosOffset))
- (maybe-file-error errorp errnum))))
-
- ; Returns two values: the number of bytes actually written, and the
- ; location of the file mark.
- (defun fsWrite (paramBlock count buffer &optional (offset 0) (errorp t))
- (setf (pref paramBlock :hparamblockrec.ioBuffer) (%inc-ptr buffer offset)
- (pref paramBlock :hparamblockrec.ioReqCount) count)
- (#_Write paramBlock)
- (setf (pref paramBlock :hparamblockrec.ioPosMode) #$fsAtMark)
- (let ((errnum (pref paramBlock :hparamblockrec.ioResult)))
- (if (or (eql #$noErr errnum) (eql #$eofErr errnum))
- (values (pref paramBlock :hparamblockrec.ioActCount)
- (pref paramBlock :hparamblockrec.ioPosOffset))
- (maybe-file-error errorp errnum))))
-
- (defun setFPos (paramBlock pos)
- (setf (pref paramBlock :hparamblockrec.ioPosOffset) pos
- (pref paramblock :hparamblockrec.ioPosMode) #$fsFromStart)
- pos)
-
- (defun getFPos (paramBlock)
- (pref paramBlock :hparamblockrec.ioPosOffset))
-
- (defun getEOF (paramBlock &optional (errorp t))
- (let* ((errnum (#_GetEOF paramBlock)))
- (if (eql #$noErr errnum)
- (%ptr-to-int (pref paramblock :hparamblockrec.ioMisc))
- (maybe-file-error errorp errnum))))
-
- (defun GetVInfo (&key (volName "") (vRefNum 0))
- (let* ((vol-pathname (truename (make-pathname :type nil :name nil :defaults volName)))
- (directory (pathname-directory vol-pathname)))
- (assert (and directory (eq :absolute (car directory))))
- (rlet ((paramBlock :hparamblockrec))
- (with-returned-pstrs ((pname (cadr directory)))
- (setf (pref paramblock :hparamblockrec.ioCompletion) (%null-ptr)
- (pref paramblock :hparamblockrec.ioNamePtr) pname
- (pref paramblock :hparamblockrec.ioVRefNum) vRefNum
- (pref paramblock :hparamblockrec.ioVolIndex) 0)
- (values (#_PBHGetVInfo paramBlock)
- (* (pref paramblock :hparamblockrec.ioVAlBlkSiz)
- (pref paramblock :hparamblockrec.ioVFrBlk))
- (pref paramblock :hparamblockrec.ioVRefNum)
- (%get-string (pref paramblock :hparamblockrec.ioNamePtr)))))))
-
- (defun maybe-file-error (errorp errnum &optional filename)
- (if errorp
- (%err-disp errnum filename)
- (values nil errnum)))
-
- (provide :mac-file-io)